<?php

namespace app\controller;

use think\Controller;
use think\Config;
use think\Db;

class Base extends Controller
{


    protected  $_site_name;
    protected  $_site_description;
    protected  $_site_domain;
    protected  $_domain;
    protected  $_att_domain;
    protected  $_att_domain_protocol;
    protected  $_user_id;
    protected  $_user_info;
    protected  $_is_win = false;
    protected $_categories = [];
    protected $_category_ids;
    protected $_category_names;
    protected $_ip_address;
    protected $_category_types = [
        '電腦桌布' => 'desktop',
        '手機桌布' => 'mobile',
        '動態電腦桌布' => 'dynamic_desktop',
        '動態手機桌布' => 'dynamic_mobile'
        ];
    protected $_type_names = [
        'desktop' => '電腦桌布',
        'mobile' => '手機桌布',
        'dynamic_desktop' => '動態電腦桌布',
        'dynamic_mobile' => '動態手機桌布'
    ];
    

    protected function _initialize()
    {
        Config::load(APP_PATH . 'extra/app.php');
        $req = request();
        $this->_domain = $req->domain();
        $this->assign('domain', $this->_domain);

        $this->_site_name =  Config::get('site_name');
        $this->assign("site_name",$this->_site_name);

        $this->_site_domain =  Config::get('site_domain');
        $this->assign("site_domain",$this->_site_domain);

        $this->_site_description =  Config::get('site_description');
        $this->assign("site_description",$this->_site_description);

        $this->assign('site_domain', Config::get('site_domain'));
        $this->_att_domain = Config::get('att_domain');
        $this->_att_domain_protocol = 'https://';
        if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN'){
            $this->_att_domain = 'ww.cc/res';
            $this->_att_domain_protocol = 'http://';
            $this->_is_win = true;
        }
        $this->assign('att_domain', $this->_att_domain);
        $this->assign('att_domain_protocol', $this->_att_domain_protocol);


        // 获取所有分类供导航使用
        $this->_categories = Db::name('category')->select();
        $this->_category_ids = array_column($this->_categories, null,'id');
        $this->_category_names = array_column($this->_categories, null,'name');
        $this->assign('categories', $this->_categories);


        $this->assign("page_title","");
        $this->assign("page_keywords","");
        $this->assign("page_description","");

        $user_service = new \app\service\UserService();
        $this->_user_id = $user_service->getUserId();
        $this->assign('user_id',$this->_user_id);

        $this->_user_info = $user_service->getUserInfo();
        $this->assign('user_info',$this->_user_info);

        try{
            $this->_ip_address = $this->request->ip();
        }catch(\Exception $e){
            $this->_ip_address = '';
        }
        $google_login_url = '';
        if (empty($this->_user_info['google_id'])){
            $google_login_url = $this->getGoogleLoginUrl();
        }
        $this->assign('google_login_url',$google_login_url);

      }

      protected function _get_user_id(){
        $user_id = cookie('user_id');
        
        // 如果没有user_id，生成一个并设置cookie
        if (!$user_id) {
            $user_id = date('YmdHis');  // 使用时间戳作为用户ID
            cookie('user_id', $user_id, [
                'path' => '/',
            ]);
        }
        return $user_id;
    }

    /**
     * Get Google login URL
     * @return string
     */
    protected function getGoogleLoginUrl()
    {
        $googleConfig = Config::get('app.google');
        
        $clientId = $googleConfig['client_id'];
        $redirectUri = $this->_domain . '/auth/googleCallback';
        
        $params = [
            'client_id' => $clientId,
            'redirect_uri' => $redirectUri,
            'response_type' => 'code',
            'scope' => 'email profile',
            'access_type' => 'online',
            'prompt' => 'select_account'
        ];
        
        return 'https://accounts.google.com/o/oauth2/v2/auth?' . http_build_query($params);
    }

     /**
     * JSON response helper
     */
    protected function jsonResponse($data = [], $code = 200, $message = 'Success')
    {
        return json([
            'code' => $code,
            'message' => $message,
            'data' => $data,
            'timestamp' => time()
        ]);
    }

    
    protected function _formatWallpaper($wallpapers){
        foreach ($wallpapers as &$wallpaper) {
            $wallpaper['tags'] = explode(',', $wallpaper['tag_name']);
            $wallpaper['category_info'] = $this->_category_ids[$wallpaper['category_id']];
            $wallpaper['category_name'] = $this->_category_ids[$wallpaper['category_id']]['name'];
            if (isset($wallpaper['file_path'])){
                $file_path = json_decode($wallpaper['file_path'],true);
                if ($wallpaper['type'] == 'mobile'){
                    $show_file_path = isset($file_path['640']) ? $file_path['640'] : (isset($file_path['1k']) ? $file_path['1k'] : (isset($file_path['2k']) ? $file_path['2k'] : $file_path['4k']));
                    $wallpaper['show_file_path'] = strpos($show_file_path,'http') === false ? $this->_att_domain_protocol.$this->_att_domain.$show_file_path : $show_file_path;
                    $thumbnail = isset($file_path['320']) ? $file_path['320'] : (isset($file_path['640']) ? $file_path['640'] : (isset($file_path['1k']) ? $file_path['1k'] : (isset($file_path['2k']) ? $file_path['2k'] : $file_path['4k'])));
                    $wallpaper['thumbnail'] = strpos($thumbnail,'http') === false ? $this->_att_domain_protocol.$this->_att_domain.$thumbnail : $thumbnail;
                }else if ($wallpaper['type'] == 'desktop'){
                    $show_file_path = isset($file_path['mid_jpg']) ? $file_path['mid_jpg'] : (isset($file_path['jpg']) ? $file_path['jpg'] : '');
                    $wallpaper['show_file_path'] = strpos($show_file_path,'http') === false ? $this->_att_domain_protocol.$this->_att_domain.$show_file_path : $show_file_path;
                    $thumbnail = isset($file_path['small_jpg']) ? $file_path['small_jpg'] : (isset($file_path['mid_jpg']) ? $file_path['mid_jpg'] : (isset($file_path['jpg']) ? $file_path['jpg'] : ''));
                    $wallpaper['thumbnail'] = strpos($thumbnail,'http') === false ? $this->_att_domain_protocol.$this->_att_domain.$thumbnail : $thumbnail;
                    $wallpaper['file_path'] = json_encode(['4k'=> isset($file_path['jpg']) ? $file_path['jpg'] : '']);
                }else{
                    $show_file_path = isset($file_path['mid_jpg']) ? $file_path['mid_jpg'] : '';
                    $wallpaper['show_file_path'] = strpos($show_file_path,'http') === false ? $this->_att_domain_protocol.$this->_att_domain.$show_file_path : $show_file_path;
                    $thumbnail = isset($file_path['small_jpg']) ? $file_path['small_jpg'] : (isset($file_path['mid_jpg']) ? $file_path['mid_jpg'] : '');
                    $wallpaper['thumbnail'] = strpos($thumbnail,'http') === false ? $this->_att_domain_protocol.$this->_att_domain.$thumbnail : $thumbnail;
                    $wallpaper['file_path'] = json_encode(['mp4'=> $file_path['mp4'],'mp4_preview'=> (isset($file_path['mp4_preview']) ? $file_path['mp4_preview'] : $file_path['mp4'])]);
                    $wallpaper['mp4_preview'] = strpos($file_path['mp4_preview'],'http') === false ? $this->_att_domain_protocol.$this->_att_domain.$file_path['mp4_preview'] : $file_path['mp4_preview'];
                }
            }
            $wallpaper['type_name'] = $this->_type_names[$wallpaper['type']];
            $wallpaper['is_dynamic'] = $wallpaper['type'] == 'dynamic_mobile' || $wallpaper['type'] == 'dynamic_desktop';
        
        }
        // debug_dump($wallpapers);
        return $wallpapers;
    }


}
